package com.yadong.smarthome2.protocol2;

import java.util.UUID;

import android.text.TextUtils;

public class Protocol {
    /**
     * 所有约定如下.
     * 1. 所有请求格式 参照 RequestProtocol.proto
     * 2. 所有返回格式参照 ResultProtocol.proto
     * 3. Function 随 Result 返回
     * 4. Function 中Argument limit 约定如下, 以
     *      a. 以o开头, 表示参数只能从后面带的选项中选择
     *      b. 以r开头, 表示参数限制为一个范围,格式为: 开始值,横线,结束值
     *    Function 中Argument type 约定如下
     *      'Z' for boolean
     *      'I' for int
     *      'J' for long
     *      'F' for float
     *      'S' for string
     */
    public static final String REQUEST_PREFIX = ">>";
    public static final String RESULT_REFIX = "<<";

    public static final String REQUEST_FOR_DEVICE_INFO = "q_functions";
    public static final String ITEM_SPLITER = ",";
    public static final String REQUEST_CONTROLL = "controll:";
    public static final String RESULT_OK = "ok";
    public static final String RESULT_FAILD = "faild";
    public static final String BOOLEAN_TRUE = "true";
    public static final String BOOLEAN_FALSE = "false";
    public static final String BLUETOOTH_NAME = "com.yadong.homeserver.bluetooth";
    public static final UUID BLUETOOTH_UUID = UUID.fromString("2d3a5deb-95b2-4334-b5e9-2ea08d1d6b8f"); 

    public static String createRequest(String request) {
        return REQUEST_PREFIX + System.currentTimeMillis() + request;
    }

    public static String createResponse(String tag, String response) {
        return RESULT_REFIX + tag + response;
    }

    public static boolean isControllRequest(String request) {
        return request.contains(REQUEST_CONTROLL);
    }

    public static boolean isDeviceInfoResult(String message) {
        return !TextUtils.isEmpty(message) && 
                    message.contains(REQUEST_FOR_DEVICE_INFO);
    }

    public static boolean isResponse(String message) {
        return !TextUtils.isEmpty(message) &&
                message.startsWith(RESULT_REFIX);
    }

    public static boolean isRequest(String message) {
        return !TextUtils.isEmpty(message) &&
                message.startsWith(REQUEST_PREFIX);
    }

    public static String getRequestTag(String request) {
        String requstWithNoPrefix = request.substring(REQUEST_PREFIX.length());
        int timeLength = (System.currentTimeMillis()+"").length();
        return requstWithNoPrefix.substring(0, timeLength);
    }

    public static String getResponseTag(String response) {
        String responseWithNoPrefix = response.substring(RESULT_REFIX.length());
        int timeLength = (System.currentTimeMillis()+"").length();
        return responseWithNoPrefix.substring(0, timeLength);
    }
}
